import uuid

from rest_framework import serializers
from django.contrib.auth.hashers import make_password

from rsa import decripta_data
from .models import Users


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = Users
        fields = ['id', 'name', 'username', 'sex', 'birthday', 'mail', 'phone', 'password']
        extra_kwargs = {
            'password': {'write_only': True},  # 密码字段不应该在响应中返回
        }

    def validate_username(self, value):
        if Users.objects.filter(username=value).exists():
            raise serializers.ValidationError("用户已存在")
        return value

    # 重写 create 方法
    def create(self, validated_data):
        return super(UserSerializer, self).create(validated_data)

    def update(self, instance, validated_data):
        return super(UserSerializer, self).update(instance, validated_data)
